/*** 
This do-file plots weekly variance of spending
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"

* Create required subfolders
cap mkdir "${root}/results/Policy"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/Policy"

*-------------------------------------------------------------------------------
* Load and process data 
*-------------------------------------------------------------------------------

* Import data
project, uses("${root}/data/derived/Policy/Affinity - National - Daily - demeaned.dta")
use "${root}/data/derived/Policy/Affinity - National - Daily - demeaned.dta", clear

keep if date <= mdy(4, 11, 2021) 

* Confirm structure 
gisid income_q date 
assert inrange(income_q, 1, 5) 

* Create values from previous year
gen temp = dow_resid_2019 if year(date) == 2019 
gen month = month(date) 
gegen previous_value = mean(temp), by(month day income_q) 
assert temp == previous_value if year(date) == 2019
drop temp 

* First-difference w.r.t. values from 2019
gen first_diff = dow_resid_2019 - previous_value 

* Start on March 1 2020
keep if date >= mdy(3, 1, 2020)

* Rescale 
replace first_diff = 100 * first_diff 

* Create 7 day moving weekly variance 
gen week = week(date)  
gen year = year(date) 

* Pooled estimates 
keep if income_q == 5

gen count = _n
gen mean = .
gen var = .

* Find the variance of the 7 days prior
forvalues var = 7/407 {
	sum first_diff if inrange(count, `var' - 6, `var')
	replace mean = `r(mean)' if count == `var'
	replace var = `r(sd)' ^ 2 if count == `var'
}

* Adjust thanksgiving outliers - these are way outside the range of our graph so we'll rescale them such that they fit between 800-1000 p.p. 
* and adjust the axis in the graph to reflect the unscaled values
local thanksgiving_graph_max = 2000
sum var if inrange(date, mdy(11, 26, 2020), mdy(12, 3, 2020))
assert inrange(`r(min)', 800, 999) & inrange(`r(max)', 1000, `thanksgiving_graph_max')
local thanksgiving_min: di %3.0f `r(min)'
local thanksgiving_max: di %4.0f `r(max)'

replace var = 800 + (log(var) - log(800)) / (log(`thanksgiving_graph_max') - log(800)) * (1000 - 800) if inrange(date, mdy(11, 26, 2020), mdy(12, 3, 2020))
assert inrange(var, 800, 1000) if inrange(date, mdy(11, 26, 2020), mdy(12, 3, 2020))

* Generate variables for the windows/bars
gen date_new = date + 0.5
gen top = 1000
gen bottom = 0

* Omit the points a few days after holidays (trailing variance) 
replace var = . if inrange(date, mdy(2, 20, 2021), mdy(2, 22, 2021)) | ///
inrange(date, mdy(6, 1, 2020), mdy(6, 3, 2020)) | inrange(date, mdy(7, 7, 2020), mdy(7, 9, 2020)) | ///
inrange(date, mdy(9, 8, 2020), mdy(9, 13, 2020)) | inrange(date, mdy(12, 6, 2020), mdy(12, 8, 2020)) | ///
inrange(date, mdy(1, 5, 2021), mdy(1, 7, 2021))

* stim1: 3/21-4/15-5/9 (4/14 break), stim2: 12/4-12/15 1/4-1/19, stim3: 2/19-3/17-4/10 (3/13-16 break)
* extend december window back by a week since this is a 7-day look-back (start Dec 15, 7 days before Dec 22 which is when it gets higher)

*-------------------------------------------------------------------------------
* Plot
*-------------------------------------------------------------------------------

tw ///
(scatter var date if income_q == 5, msize(vsmall)) ///
	(rarea top bottom date_new if income_q == 5 & inrange(date, mdy(3, 21, 2020), mdy(4, 14, 2020)), color(oi1%20) lwidth(none)) ///      stim 1 pre-period
	(rarea top bottom date_new if income_q == 5 & inrange(date, mdy(12, 4, 2020), mdy(12, 15, 2020)), color(oi1%20) lwidth(none)) ///     stim 2 pre-period 
	(rarea top bottom date_new if income_q == 5 & inrange(date, mdy(2, 20, 2021), mdy(3, 13, 2021)), color(oi1%20) lwidth(none)) ///      stim 3 pre-period 
	(rarea top bottom date_new if income_q == 5 & inrange(date, mdy(4, 15, 2020), mdy(5, 9, 2020)), color(oi1%50) lwidth(none)) ///       stim 1 post-period
	(rarea top bottom date_new if income_q == 5 & inrange(date, mdy(1, 4, 2021), mdy(1, 19, 2021)), color(oi1%50) lwidth(none)) ///       stim 2 post-period
	(rarea top bottom date_new if income_q == 5 & inrange(date, mdy(3, 17, 2021), mdy(4, 10, 2021)), color(oi1%50) lwidth(none)) ///      stim 3 post-period 
	(rarea top bottom date_new if income_q == 5 & inrange(date, mdy(5, 23, 2020), mdy(6, 1, 2020)), color(oi8%50) lwidth(none)) ///       memorial day week
	(rarea top bottom date_new if income_q == 5 & inrange(date, mdy(7, 1, 2020), mdy(7, 7, 2020)), color(oi8%50) lwidth(none)) ///        July 4 week
	(rarea top bottom date_new if income_q == 5 & inrange(date, mdy(8, 31, 2020), mdy(9, 8, 2020)), color(oi8%50) lwidth(none)) ///       Labor Day week
	(rarea top bottom date_new if income_q == 5 & inrange(date, mdy(11, 24, 2020), mdy(12, 5, 2020)), color(oi8%50) lwidth(none)) ///     Thanksgiving period
	(rarea top bottom date_new if income_q == 5 & inrange(date, mdy(12, 15, 2020), mdy(1, 3, 2021)), color(oi8%50) lwidth(none)) ///      Christmas period  
	(rarea top bottom date_new if income_q == 5 & inrange(date, mdy(2, 12, 2021), mdy(2, 19, 2021)), color(oi8%50) lwidth(none)) ///      president's day week
	, ///
	text(1070 `=mdy(4, 15, 2020)' "Stimulus 1", color(gs8) size(small)) /// 
	xline(`=mdy(4, 15, 2020)', lcolor(gs8) lpattern(dash) noextend) ///
	text(1070 `=mdy(1, 4, 2021)' "Stimulus 2", color(gs8) size(small)) /// 
	xline(`=mdy(1, 4, 2021)', lcolor(gs8) lpattern(dash) noextend) ///
	text(1070 `=mdy(3, 17, 2021)' "Stimulus 3", color(gs8) size(small)) /// 
	xline(`=mdy(3, 17, 2021)', lcolor(gs8) lpattern(dash) noextend) ///
	text(1020 `=mdy(2, 15, 2021)' "President's Day", color(gs8) size(vsmall)) /// 
	text(1020 `=mdy(5, 27, 2020)' "Memorial Day", color(gs8) size(vsmall)) /// 
	text(1020 `=mdy(12, 27, 2020)' "Christmas", color(gs8) size(vsmall)) /// 
	text(1020 `=mdy(11, 20, 2020)' "Thanksgiving", color(gs8) size(vsmall)) /// 
	text(1020 `=mdy(9, 5, 2020)' "Labor Day", color(gs8) size(vsmall)) /// 
	text(1020 `=mdy(7, 4, 2020)' "July 4", color(gs8) size(vsmall)) /// 
	ylab(0 "0 p.p. " 200 "200 p.p. " 400 "400 p.p. " 600 "600 p.p. " 800 "800 p.p. " 1000 "`thanksgiving_graph_max' p.p. ", labsize(small) nogrid noticks) ///
	xlab(`=mdy(3, 1, 2020)' "Mar 2020" `=mdy(5, 1, 2020)' "May 2020" `=mdy(7, 1, 2020)' "Jul 2020" `=mdy(9, 1, 2020)' "Sep 2020" `=mdy(11, 1, 2020)' "Nov 2020" ///
	`=mdy(1, 1, 2021)' "Jan 2021" `=mdy(3, 1, 2021)' "Mar 2021", labsize(small)) ///
	ytitle("Within-Week Variance of Daily Change in Indexed Spending (p.p.)" "First-Difference Relative to 2019, Residualized DoW FEs", size(small)) /// 
	xtitle("") ///
	title(" ", size(large)) ///
	legend(order(2 "Stimulus Pre-Period" 5 "Stimulus Post-Period" 8 "Holiday Period") cols(3) size(small)) ///
	ytick(0 200 400 600 1000) ///
	text(810 `=mdy(2, 23, 2020)' "\", orientation(vertical) color(gs7) size(2.5)) ///
	text(790 `=mdy(2, 23, 2020)' "\", orientation(vertical) color(gs7) size(2.5)) ///
	text(900 `=mdy(2, 15, 2020)' "log scale", orientation(vertical) color(gs7) size(1.5))
	
	oi_graph_export "${root}/results/Policy/scatter variance after first-differencing 7-day moving",  type(${fig_type})

* Scalars
cap erase "${root}/results/paper numbers/Policy/Policy - Variance in Spending 7-day Moving Average.yaml"

yamlout using "${root}/results/paper numbers/Policy/Policy - Variance in Spending 7-day Moving Average.yaml", ///
	key("thanksgiving_min") ///
	comment("Min. variance of consumer spending during Thanksgiving 2020") ///
	value("`thanksgiving_min'")

yamlout using "${root}/results/paper numbers/Policy/Policy - Variance in Spending 7-day Moving Average.yaml", ///
	key("thanksgiving_max") ///
	comment("Max. variance of consumer spending during Thanksgiving 2020") ///
	value("`thanksgiving_max'")
	
yamlout using "${root}/results/paper numbers/Policy/Policy - Variance in Spending 7-day Moving Average.yaml", ///
	key("thanksgiving_graph_max") ///
	comment("Max. variance of consumer spending on graph") ///
	value("`thanksgiving_graph_max'")